#!/bin/bash
  
# Run the drop.sql batch file to drop existing tables
# Inside the drop.sql, you sould check whether the table exists. Drop them ONLY if they exists. 
mysql CS144 < drop.sql
  
# Run the create.sql batch file to create the database and tables
mysql CS144 < create.sql

# Compile and run the parser to generate the appropriate load files
ant
ant run-all

# Removal duplicates in load files
cat temp_users.csv | awk '{print length, $0}' | sort -n | awk '{$1=""; print $0 }' | sort -u | sed 's/^ \(.*\)$/\1/g' | sed '
/^"[^"]*","[^"]*","",""/ { 
N 
/^\("[^"]*"\)[^\n]*\n\1.*/ D }' > temp_output.csv
mv temp_output.csv temp_users.csv

cat temp_catitems.csv | sort -u > temp_output.csv
mv temp_output.csv temp_catitems.csv

# Run the load.sql batch file to load the data
mysql CS144 < load.sql

# Remove all temporary files
rm -r ./bin
rm -r ./lib
rm temp_bids.csv
rm temp_categories.csv
rm temp_catitems.csv
rm temp_items.csv
rm temp_users.csv
